package com.atguigu.szdz.dcsyl.service.impl;

import com.atguigu.szdz.dcsyl.service.SysService;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("sysService")
public class SysServiceImpl implements SysService {
    @Autowired
    private MapperUtils mapperUtils;

    @Override
    public List<Map<String, Object>> selectUserByUserName(String userName) {
        return mapperUtils.queryBySql("select * from sys_user where username='" + userName + "'");
    }

    @Override
    public void add(Map<String, String> params) {
        String userName = params.get("userName");
        String finalPassword = params.get("finalPassword");
        String salt = params.get("salt");
        String sql = "insert into sys_user (username,password,salt) VALUES ('" + userName + "','" + finalPassword + "','" + salt + "')";
        mapperUtils.updataBySql(sql);
    }

    @Override
    public List<Map<String, Object>> getMuneTree() {
        List<Map<String, Object>> list = mapperUtils.queryBySql("select * from sys_menu where parent_id='0' order by order_num");
        list = getChildMuneTree(list);
        return list;
    }

    @Override
    public Integer addMune(Map<String, String> params) {
        String label = params.get("label");
        String parent_id = params.get("parent_id");
        String icon = params.get("icon");
        String order_num = params.get("order_num");
        String path = params.get("path");
        String url = params.get("url");
        String sql = "insert into sys_menu (label,parent_id,icon, order_num, path, url)" +
                " values ('" + label + "','" + parent_id + "','" + icon + "','" + order_num + "','" + path + "','" + url + "')";
        return mapperUtils.updataBySql(sql);
    }

    @Override
    public Integer editMune(Map<String, String> params) {
        String id = params.get("id");
        String label = params.get("label");
        String parent_id = params.get("parent_id");
        String icon = params.get("icon");
        String order_num = params.get("order_num");
        String path = params.get("path");
        String url = params.get("url");
        String sql = "update sys_menu set label='" + label + "',parent_id='" + parent_id + "',icon='" + icon
                + "',order_num='" + order_num + "',path='" + path + "',url='" + url + "' where id='" + id + "'";

        return mapperUtils.updataBySql(sql);
    }

    @Override
    public Integer deleteMune(String id) {
        List<Map<String, Object>> list = mapperUtils.queryBySql("select * from sys_menu where parent_id='" + id + "'");

        if (list.size() <= 0 || list == null) {
            return mapperUtils.updataBySql("delete from sys_menu where id='" + id + "'");
        }

        return 500;
    }

    //获取菜单的子菜单
    public List<Map<String, Object>> getChildMuneTree(List<Map<String, Object>> list) {
        List<Map<String, Object>> list2 = new ArrayList<>();
        for (Map<String, Object> map : list) {
            int id = (int) map.get("id");
            List<Map<String, Object>> childrenList = mapperUtils.queryBySql("select * from sys_menu where parent_id='" + id + "' order by order_num");
            if (childrenList.size() > 0) {
                String parentLable = (String) map.get("label");
                System.out.println("ppppppppppppppp:" + parentLable);
                List<Map<String, Object>> childrenList2 = new ArrayList<>();
                for (Map<String, Object> childMap : childrenList) {
                    childMap.put("parentLable", parentLable);
                    System.out.println("cccccccccccccc:" + childMap);
                    childrenList2.add(childMap);
                }
                childrenList = childrenList2;
                childrenList = getChildMuneTree(childrenList);
                map.put("children", childrenList);
            }
            list2.add(map);
        }
        return list2;
    }
}
